package net.yeah.mickey_zhouyou.database.util;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.TransformerException;

import net.yeah.mickey_zhouyou.database.domain.BaseEntity;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class XMLUtilTest {

	static void testPrint() throws TransformerException, IOException {
		ABC abc = new ABC(123, 345, new java.util.Date(), "abcd efg 中国");
		Document doc = XMLUtil.toDoc(abc);

		System.out.println(XMLUtil.docToXMLString(doc));
	}

	static void testSaveFile() throws TransformerException, IOException {
		ABC abc = new ABC(123, 345, new java.util.Date(), "第一个");
		Document doc = XMLUtil.toDoc(abc);
		File f = new File("a.xml");
		XMLUtil.saveDocument(doc, f);
	}

	static void testSaveFile2() throws TransformerException, IOException,
			SAXException {
		testSaveFile();
		Document doc = XMLUtil.loadDoc(new File("a.xml"));
		System.out.println(XMLUtil.docToXMLString(doc));
	}

	static void testXmlToObj() throws Exception {
		testSaveFile();
		Document doc = XMLUtil.loadDoc(new File("a.xml"));
		BaseEntity abc = XMLUtil.toEntity(doc);
		Document doc2 = XMLUtil.toDoc(abc);

		String s1 = XMLUtil.docToXMLString(doc);
		String s2 = XMLUtil.docToXMLString(doc2);

		System.out.println(s1);
		System.out.println(s2);

		System.out.println(s1.equals(s2));
	}

	public static void main(String[] args) throws Exception {
		// testPrint();
		// testSaveFile2();
		testXmlToObj();
	}

	static class ABC extends BaseEntity {
		private int i;
		private long l;
		private java.util.Date d;
		private String s;

		ABC() {
		}

		ABC(int i, long l, java.util.Date d, String s) {
			this.i = i;
			this.l = l;
			this.d = d;
			this.s = s;
		}

		@Override
		public int compare(BaseEntity o1, BaseEntity o2) {
			// TODO Auto-generated method stub
			return 0;
		}
	}

}
